import LXCOM from '../common/common.js'
import absgraph from './devicegraph.js'
import { CoordTRFUtil } from '../common/tools/coordtool.js'
import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;

import DevGraphInfoPkg from '../common/struct/basestruct/devgraphinfo.js'
var DevGraphInfo = DevGraphInfoPkg.DevGraphInfo;

import {LGXEnums}  from '../common/sysenum.js'
import coord from '../common/struct/coord.js'


class ContainerMoldGraph extends absgraph.DeviceGraph {
    constructor(type) {
        super(type);
        var pos = new coord.PointPos();
        this.setPos(pos);
        this.m_graphType = LGXEnums.LGXGraphType.Container_type;
        this.m_preCurentModifingFrameHornInfo = null;//每次修改前，记录变更前外框横向特征标记
        this.m_preCurentModifingFrameVertInfo = null;//每次修改前，记录变更前外框纵向特征标记
    }

    display(map, mapInfo,forClip) {
        var position = this.getPos();
        map.save();
        var symbol = this.getSymbol();
        if (symbol != null && symbol.getMoldActionType() == LGXEnums.moldActionType.common) {
            let symCnt = symbol.getSymScope().getCnt();
            let x = position.x + symCnt.x;
            let y = position.y + symCnt.y;
            var graphCnt = { x, y };
            var pt = CoordTRFUtil.world2Screen(position, mapInfo);
            pt1 = CoordTRFUtil.world2Screen(graphCnt, mapInfo);
            tx = pt.x;
            ty = pt.y;

            let scalex = this.getScaleX();
            let scaley = this.getScaleY();
            let rotv = this.getRadAngle();

            map.translate(pt.x, pt.y);
            map.rotate(-rotv);
            map.scale(scalex, scaley);
            symbol.display(map, this, pt, mapInfo);
        }
        map.restore();
        if (this._selected) {
            this.displayFeaturePoint(map, mapInfo);
        }
        super.displayGraphScope(map, mapInfo);
    }

    displayFeaturePoint(map, mapInfo) {
        if (this._useful != 1 || this._finishFlag != 1) {
            return;
        }
        var featurePTlst = this.getFeaturePtList();
        let ptlst = [];
        featurePTlst.forEach(p => {
            ptlst.push(p.pt);
        });
        this.displayDashLineRectScope(ptlst, map, mapInfo)
        ptlst.length = 0;
        ptlst = null;
    }

    displayDashLineRectScope(ptlst, map, mapInfo) {
        if (ptlst.length != 9) {
            return;
        }
        map.save();
        map.setLineDash([5, 5]);
        map.beginPath();
        map.lineWidth = 2;
        map.strokeStyle = "#ffffff";
        var pt0 = ptlst[0];
        var p0 = CoordTRFUtil.world2Screen(pt0, mapInfo);
        map.moveTo(p0.x, p0.y);
        for (var i = 1; i < 4; i++) {
            var pt = ptlst[i];
            var p = CoordTRFUtil.world2Screen(pt, mapInfo);
            map.lineTo(p.x, p.y);
        }
        map.lineTo(p0.x, p0.y);
        map.closePath();
        map.stroke();
        for (var i = 0; i < 9; i++) {
            var pt = ptlst[i];
            var p = CoordTRFUtil.world2Screen(pt, mapInfo);
            this.drawFocusPoint(map, p.x, p.y, "#0000ff");
        }

        map.restore();
    }

    distance(mousePt, scaling, mapscale) {
        let dis = -1;
        if (this.getStart() == 1 && this.getFinish() == 1) {
            if (super.preCheckInScope(mousePt, scaling)) {
                let seleKind = this.calculateSubKind(mousePt, scaling, mapscale);
                if (seleKind > 0) {
                    dis = 0;
                }
            }
        }
        return dis;
    }

    calScope(mapscale) {
        var position = this.getPos();
        let rect = this.calculateZone(position);
        if (rect) {
            this._zone.clone(rect);
        }
    }

    getLocalZone() {
        var position = this.getPos();
        let rect = this.calculateZone(position);
        rect.left -= position.x;
        rect.bottom -= position.y;
        rect.right -= position.x;
        rect.top -= position.y;
        return rect;
    }

    calculateZone(devPos) {
        let rect = null;
        var symbol = this.getSymbol();
        if (symbol) {
            if (this.getChildList().length == 0) {
                rect = this.calculateZoneRefSymbol(devPos, symbol);
            }
            else {
                rect = new LXCOM.LGXRect(0, 0, 0, 0);
                let frameRectGraph = this.getFrameRect();
                let frameRect = frameRectGraph.getZone();
                rect.left = frameRect.left;
                rect.right = frameRect.right;
                rect.bottom = frameRect.bottom;
                rect.top = frameRect.top;
            }
        }
        else {
            rect = new LXCOM.LGXRect(0, 0, 0, 0);
            var offsetGap = 5;
            rect.left = (devPos.x - offsetGap);
            rect.bottom = (devPos.y - offsetGap);
            rect.right = (devPos.x + offsetGap);
            rect.top = (devPos.y + offsetGap);
        }
        return rect;
    }

    calculateSubControlPoint(mousePt, scaling, mapscale) {
        let seleKind = this.calculateSubKind(mousePt, scaling, mapscale);
        return seleKind;
    }

    calculateSubKind(mousePt, scaling, mapscale) {
        let selKind = 0;
        var featurePTlst = this.getFeaturePtList();
        var count = featurePTlst.length;
        for (var i = 0; i < count; i++) {
            let ptInfo = featurePTlst[i];
            var dis = funcs.distancePoint(mousePt, ptInfo.pt, scaling);
            if (dis < global.SelectGap) {
                selKind = ptInfo.kind;
                console.log("kind=" + selKind);
                break;
            }
        }
        if (selKind == 0) {
            let frameRect = this.getFrameRect();
            if (frameRect) {
                let ptList = frameRect.getPosCopy().posList;
                let p0 = ptList[0];
                ptList.push(p0);
                var cmpGap = global.SelectGap;
                let flag = funcs.chkPtOnPolyline(mousePt, ptList, cmpGap, scaling);
                if (flag) {
                    selKind = 9;
                }
            }
        }

        return selKind;
    }

    movePosOffset(offsetPt, modCmdInfo) {
        console.log("selkind=" + this._selKind);
        console.log("tx=" + offsetPt.x, "ty=" + offsetPt.y);
        let selKind = this.getSelKind();
        if (selKind == 9) {
            this.translate(offsetPt, modCmdInfo.mapscale, false);
        }
        else if (selKind >= 1 && selKind <= 8) {
            this.moveFeaturePt(selKind, offsetPt, modCmdInfo.mapscale);
        }
    }

    moveFeaturePt(selKind, offsetPt, mapscale) {
        let frameRect = this.getFrameRect();
        if (!frameRect) {
            return;
        }
        switch (selKind) {
            case 1:
                this.dragLeftTopPt(frameRect, offsetPt, mapscale, selKind);
                break;

            case 2:
                this.dragRightTopPt(frameRect, offsetPt, mapscale, selKind);
                break;

            case 3:
                this.dragRightBottomPt(frameRect, offsetPt, mapscale, selKind);
                break;

            case 4:
                this.dragLeftBottomPt(frameRect, offsetPt, mapscale, selKind);
                break;

            case 5:
                this.dragTopMidPt(frameRect, offsetPt, mapscale, selKind);
                break;

            case 6:
                this.dragRightMidPt(frameRect, offsetPt, mapscale, selKind);
                break;

            case 7:
                this.dragBottomMidPt(frameRect, offsetPt, mapscale, selKind);
                break;

            case 8:
                this.dragLeftMidPt(frameRect, offsetPt, mapscale, selKind);
                break;
        }
    }

    dragLeftTopPt(frameRectGraph, offsetPt, mapscale, selKind) {
        let frameRect = frameRectGraph.getZone();
        let ltPt = frameRect.ltPt();
        ltPt.x += offsetPt.x;
        ltPt.y += offsetPt.y;

        let rbPt = frameRect.rbPt();
        this.correctRectPos(frameRectGraph, ltPt, rbPt, selKind);
    }

    dragRightTopPt(frameRectGraph, offsetPt, mapscale, selKind) {
        let frameRect = frameRectGraph.getZone();
        let ltPt = frameRect.ltPt();
        ltPt.y += offsetPt.y;

        let rbPt = frameRect.rbPt();
        rbPt.x += offsetPt.x;
        this.correctRectPos(frameRectGraph, ltPt, rbPt, selKind);
    }

    dragRightBottomPt(frameRectGraph, offsetPt, mapscale, selKind) {
        let frameRect = frameRectGraph.getZone();
        let ltPt = frameRect.ltPt();
        let rbPt = frameRect.rbPt();
        rbPt.x += offsetPt.x;
        rbPt.y += offsetPt.y;

        this.correctRectPos(frameRectGraph, ltPt, rbPt, selKind);
    }

    dragLeftBottomPt(frameRectGraph, offsetPt, mapscale, selKind) {
        let frameRect = frameRectGraph.getZone();
        let ltPt = frameRect.ltPt();
        ltPt.x += offsetPt.x;

        let rbPt = frameRect.rbPt();
        rbPt.y += offsetPt.y;
        this.correctRectPos(frameRectGraph, ltPt, rbPt, selKind);
    }

    dragTopMidPt(frameRectGraph, offsetPt, mapscale, selKind) {
        let frameRect = frameRectGraph.getZone();
        let ltPt = frameRect.ltPt();
        ltPt.y += offsetPt.y;

        let rbPt = frameRect.rbPt();
        this.correctRectPos(frameRectGraph, ltPt, rbPt, selKind);
    }

    dragRightMidPt(frameRectGraph, offsetPt, mapscale, selKind) {
        let frameRect = frameRectGraph.getZone();
        let ltPt = frameRect.ltPt();

        let rbPt = frameRect.rbPt();
        rbPt.x += offsetPt.x;
        this.correctRectPos(frameRectGraph, ltPt, rbPt, selKind);
    }

    dragBottomMidPt(frameRectGraph, offsetPt, mapscale, selKind) {
        let frameRect = frameRectGraph.getZone();
        let ltPt = frameRect.ltPt();

        let rbPt = frameRect.rbPt();
        rbPt.y += offsetPt.y;
        this.correctRectPos(frameRectGraph, ltPt, rbPt, selKind);
    }

    dragLeftMidPt(frameRectGraph, offsetPt, mapscale, selKind) {
        let frameRect = frameRectGraph.getZone();
        let ltPt = frameRect.ltPt();
        ltPt.x += offsetPt.x;

        let rbPt = frameRect.rbPt();
        this.correctRectPos(frameRectGraph, ltPt, rbPt, selKind);
    }

    correctRectPos(frameRectGraph, ltPt, rbPt, selKind) {
        this.m_preCurentModifingFrameHornInfo = this.getGraphHornInfo(frameRectGraph);
        this.m_preCurentModifingFrameVertInfo = this.getGraphVertInfo(frameRectGraph);
        let preRect = frameRectGraph.getPosCopy().getZone();

        let newRectPos = new coord.MultiPos();
        newRectPos.addPoint(ltPt);
        newRectPos.addPoint({ x: rbPt.x, y: ltPt.y });
        newRectPos.addPoint(rbPt);
        newRectPos.addPoint({ x: ltPt.x, y: rbPt.y });
        frameRectGraph.setPos(newRectPos);

        let curentRect = frameRectGraph.getPosCopy().getZone()
        let dragInfo = this.getGraphDragInfo(preRect, curentRect, selKind);
        if (!dragInfo) {
            return;
        }

        let otherChildList = this.getNoSpecialChild();

        let frameRect = frameRectGraph.getZone();
        this._zone.left = frameRect.left;
        this._zone.right = frameRect.right;
        this._zone.bottom = frameRect.bottom;
        this._zone.top = frameRect.top;

        let cntPt = this._zone.getCnt();

        let position = this.getPos();
        position.x = cntPt.x;
        position.y = cntPt.y;
        this.coorectOtherChildPos(dragInfo, otherChildList);
    }

    getFeaturePtList() {
        var ptlst = [];
        let frameRect = this.getFrameRect();
        if (!frameRect) {
            return ptlst;
        }
        let orgWorldPt = frameRect.getZone().getCnt();
        var symScope = frameRect.getLocalZone();
        var rotv = frameRect.getRadAngle();
        var rotcnt = { x: 0, y: 0 };
        let x = symScope.left;
        let y = symScope.top;
        var lefttopPt = { x, y };
        var p1 = funcs.rotate(lefttopPt, rotcnt, rotv);
        p1.x = orgWorldPt.x + p1.x;
        p1.y = orgWorldPt.y + p1.y;

        x = symScope.right;
        y = symScope.top;
        var righttopPt = { x, y };
        var p2 = funcs.rotate(righttopPt, rotcnt, rotv);
        p2.x = orgWorldPt.x + p2.x;
        p2.y = orgWorldPt.y + p2.y;

        x = symScope.right;
        y = symScope.bottom;
        var rightbotPt = { x, y };
        var p3 = funcs.rotate(rightbotPt, rotcnt, rotv);
        p3.x = orgWorldPt.x + p3.x;
        p3.y = orgWorldPt.y + p3.y;

        x = symScope.left;
        y = symScope.bottom;
        var leftbotPt = { x, y };
        var p4 = funcs.rotate(leftbotPt, rotcnt, rotv);
        p4.x = orgWorldPt.x + p4.x;
        p4.y = orgWorldPt.y + p4.y;


        ptlst.push({ pt: p1, kind: 1 });
        ptlst.push({ pt: p2, kind: 2 });
        ptlst.push({ pt: p3, kind: 3 });
        ptlst.push({ pt: p4, kind: 4 });

        x = (p1.x + p2.x) / 2;
        y = (p1.y + p2.y) / 2;
        let topMid = { x, y };
        ptlst.push({ pt: topMid, kind: 5 });

        x = (p2.x + p3.x) / 2;
        y = (p2.y + p3.y) / 2;
        let rightMid = { x, y };
        ptlst.push({ pt: rightMid, kind: 6 });

        x = (p3.x + p4.x) / 2;
        y = (p3.y + p4.y) / 2;
        let botMid = { x, y };
        ptlst.push({ pt: botMid, kind: 7 });

        x = (p4.x + p1.x) / 2;
        y = (p4.y + p1.y) / 2;
        let leftMid = { x, y };
        ptlst.push({ pt: leftMid, kind: 8 });

        //图符中心点
        x = symScope.getCnt().x;
        y = symScope.getCnt().y;
        var shakeHandBotPt = { x, y };
        var p5 = funcs.rotate(shakeHandBotPt, rotcnt, rotv);
        p5.x = orgWorldPt.x + p5.x;
        p5.y = orgWorldPt.y + p5.y;
        ptlst.push({ pt: p5, kind: 9 });

        return ptlst;
    }


    toInfo() {
        var info = new DevGraphInfo();
        super.commoneProp2Info(info);
        var position = this.getPos();
        info.pos.clone(position);
        info.x = position.x;
        info.y = position.y;
        info.scalex = this.getScaleX();
        info.scaley = this.getScaleY();
        if (this._refSymbol){
            info.refSymbolUUID = this._refSymbol.getUUID();
        }
        return info;
    }

    findVerticalAlignItemGraph(chkGraphTop) {
        let destGraph = null;
        let minDis2CurentTop = 0;
        let childList = this.getChildList();
        for (let i = 0; i < childList.length; i++) {
            let p = childList[i];
            if (p.m_graphType != LGXEnums.LGXGraphType.containerItem_type
                && p.m_graphType != LGXEnums.LGXGraphType.RectGraph_type) {
                continue;
            }

            let bottom = p.getZone().bottom;
            let dis = Math.abs(bottom - chkGraphTop);
            if (destGraph == null) {
                destGraph = p;
                minDis2CurentTop = dis;
            }
            else if (minDis2CurentTop > dis) {
                destGraph = p;
                minDis2CurentTop = dis;
            }
        }
        return destGraph;
    }

    //extendKind 变更坐标类型 0--XY都有，1--只有X，2--只有Y
    coorectOtherChildPos(dragInfo, otherChildList) {
        for (let i = 0; i < otherChildList.length; i++) {
            let child = otherChildList[i];
            this.coorectAliginContainerChildPos(child, dragInfo);
        }
        let unsetVerticalGraphInfoList = [];
        otherChildList.forEach(graph => {
            if (graph.m_graphType != LGXEnums.LGXGraphType.TextGraph_type){
                let vertInfo = this.getGraphVertInfo(graph);
                unsetVerticalGraphInfoList.push(vertInfo);
            }
        });

        let unsetHornGraphInfoList = [];
        otherChildList.forEach(graph => {
            if (graph.m_graphType != LGXEnums.LGXGraphType.TextGraph_type){
                let hornInfo = this.getGraphHornInfo(graph);
                unsetHornGraphInfoList.push(hornInfo);
            }
        });

        this.correctVeticalPosition_alignTop(dragInfo, otherChildList);
        this.correctVeticalPosition_alignBottom(dragInfo, otherChildList);
        this.correctAutoAlignHeightGraphBottom(unsetVerticalGraphInfoList);

        this.correctHornPosition_alignLeft(dragInfo, otherChildList);
        this.correctHornPosition_alignRight(dragInfo, otherChildList);
        this.correctAutoAlignWidthGraphRight(unsetHornGraphInfoList);
    }

    // <el-radio label=0 size="large">随容器</el-radio>
    //     <el-radio label=1 size="large">不变</el-radio>
    //     <el-radio label=2 size="large">自动对齐</el-radio>
    //     <el-radio label=3 size="large">自由调整</el-radio>
    //   </el-radio-group>

    //   <label>纵向位置</label>
    //   <el-radio-group  v-model="verticalPositionRadio" class="ml-4">
    //     <el-radio label=0 size="large">顶部随容器</el-radio>
    //     <el-radio label=1 size="large">底部随容器</el-radio>
    //     <el-radio label=2 size="large">顶部保持纵向布局对齐</el-radio>
    //     <el-radio label=3 size="large">底部保持纵向布局对齐</el-radio>
    correctVeticalPosition_alignTop(dragInfo, otherChildList) {
        let unSortedAlignTopGraphList = [];
        otherChildList.forEach(graph => {
            if (graph.m_graphType != LGXEnums.LGXGraphType.TextGraph_type){
                let verticalPostionCtrlFlag = graph.getVerticalPostionCtrlFlag();
                if (verticalPostionCtrlFlag == 0 || verticalPostionCtrlFlag == 2) {
                    unSortedAlignTopGraphList.push(graph);
                }
            }
        });

        let sortedAlignTopGraphList = this.sortGraph(unSortedAlignTopGraphList, 1, true);
        let length = sortedAlignTopGraphList.length;
        if (length > 0) {
            let topGroup = [];
            let topInfo = sortedAlignTopGraphList[0];
            topGroup.push(topInfo);

            let belowTopList = [];
            sortedAlignTopGraphList.forEach(p => {
                if (p != topInfo) {
                    let deta_maxy = Math.abs(topInfo.max.y - p.max.y);
                    if (deta_maxy < global.ZERO_4E) {
                        topGroup.push(p);
                    }
                    else {
                        belowTopList.push(p);
                    }
                }
            });
            topGroup.forEach(p => {
                let topGraph = p.graph;
                let preBottom = this.correctTopGraphVeticalPosition(dragInfo, topGraph);
                this.setAlignBottomGraph(p, preBottom, belowTopList);
            });
        }

    }

    correctAutoAlignHeightGraphBottom(unSortedAlignTopGraphInfoList) {
        let autoAlignHeightGraphList = [];
        unSortedAlignTopGraphInfoList.forEach(graphInfo => {
            let verticalSizeCtrlFlag = graphInfo.graph.getVerticalSizeCtrlFlag();
            if (verticalSizeCtrlFlag == 2) {
                autoAlignHeightGraphList.push(graphInfo);
            }
        });

        autoAlignHeightGraphList.forEach(p => {
            this.correctAutoAlignHeightGraphBottomRefOtherGraph(p, unSortedAlignTopGraphInfoList);
        });
    }

    correctAutoAlignHeightGraphBottomRefOtherGraph(curentGraphInfo, unSortedAlignTopGraphInfoList) {
        let destAlignTopGraph = null;//当前待调整底部坐标图形其底部对齐目标图形的顶部
        unSortedAlignTopGraphInfoList.forEach(p => {
            let deta = Math.abs(p.max.y - curentGraphInfo.min.y);
            if (deta < global.ZERO_4E) {
                destAlignTopGraph = p.graph;
                return;
            }
        });

        let curentGraph = curentGraphInfo.graph;
        let hornInfo = this.getGraphHornInfo(curentGraph);
        let vertInfo = this.getGraphVertInfo(curentGraph);
        if (destAlignTopGraph) {
            let destVertInfo = this.getGraphVertInfo(destAlignTopGraph);
            vertInfo.min.y = destVertInfo.max.y;
            this.restRectGraphPosition(curentGraph, hornInfo, vertInfo);
        }
        else{
            let info = this.m_preCurentModifingFrameVertInfo;
            let deta = Math.abs(info.min.y - curentGraphInfo.min.y);
            if (deta < global.ZERO_4E) {
                let destVertInfo = this.getGraphVertInfo(info.graph);
                vertInfo.min.y = destVertInfo.min.y;
                this.restRectGraphPosition(curentGraph, hornInfo, vertInfo);
            }
        }
    }

    setAlignBottomGraph(preInfo, preBottom, belowTopList) {
        let align2ThisBottomList = [];
        belowTopList.forEach(p => {
            let graph = p.graph;
            let verticalPostionCtrlFlag = graph.getVerticalPostionCtrlFlag();
            if (verticalPostionCtrlFlag == 0 || verticalPostionCtrlFlag == 2) {
                let detay = Math.abs(preInfo.min.y - p.max.y);
                if (detay < global.ZERO_4E) {
                    align2ThisBottomList.push(p);
                }
            }
        });

        align2ThisBottomList.forEach(p => {
            let graph = p.graph;
            let hornInfo = this.getGraphHornInfo(graph);
            let vertInfo = this.getGraphVertInfo(graph);
            let preHeight = Math.abs(vertInfo.max.y - vertInfo.min.y);

            vertInfo.max.y = preBottom;
            if (graph.getVerticalSizeCtrlFlag() == 1) {
                vertInfo.min.y = vertInfo.max.y - preHeight;
            }

            this.restRectGraphPosition(graph, hornInfo, vertInfo);
            this.setAlignBottomGraph(p, vertInfo.min.y, belowTopList);
        });
    }

    correctTopGraphVeticalPosition(dragInfo, topGraph) {
        let basePt = dragInfo.basePt;
        let dragPt = dragInfo.dragPt;
        let preDragPt = dragInfo.preDragPt;

        let preDistance = Math.abs(preDragPt.y - basePt.y);
        let curentDistance = Math.abs(dragPt.y - basePt.y);
        let yscale = curentDistance / preDistance;

        let hornInfo = this.getGraphHornInfo(topGraph);
        let vertInfo = this.getGraphVertInfo(topGraph);
        let preHeight = Math.abs(vertInfo.max.y - vertInfo.min.y);

        let verticalPostionCtrlFlag = topGraph.getVerticalPostionCtrlFlag();
        if (verticalPostionCtrlFlag == 0 || verticalPostionCtrlFlag == 2) {
            let predis = vertInfo.max.y - basePt.y;
            let newy = basePt.y + predis * yscale;
            vertInfo.max.y = newy;
        }
        if (topGraph.getVerticalSizeCtrlFlag() == 1) {
            vertInfo.min.y = vertInfo.max.y - preHeight;
        }
        this.restRectGraphPosition(topGraph, hornInfo, vertInfo);
        return vertInfo.min.y;
    }

    correctVeticalPosition_alignBottom(dragInfo, otherChildList) {
        let unSortedAlignTopGraphList = [];
        otherChildList.forEach(graph => {
            let verticalPostionCtrlFlag = graph.getVerticalPostionCtrlFlag();
            if (verticalPostionCtrlFlag == 1 || verticalPostionCtrlFlag == 3) {
                unSortedAlignTopGraphList.push(graph);
            }
        });
        let sortedAlignTopGraphList = this.sortGraph(unSortedAlignTopGraphList, 1, false);
        let length = sortedAlignTopGraphList.length;
        if (length > 0) {
            let bottomGroup = [];
            let bottomInfo = sortedAlignTopGraphList[0];
            bottomGroup.push(bottomInfo);

            let upBottomList = [];
            sortedAlignTopGraphList.forEach(p => {
                if (p != bottomInfo) {
                    let deta_maxy = Math.abs(bottomInfo.min.y - p.min.y);
                    if (deta_maxy < global.ZERO_4E) {
                        bottomGroup.push(p);
                    }
                    else {
                        upBottomList.push(p);
                    }
                }
            });
            bottomGroup.forEach(p => {
                let bottomGraph = p.graph;
                let preTop = this.correctBottomGraphVeticalPosition(dragInfo, bottomGraph);
                this.setAlignTopGraph(p, preTop, upBottomList);
            });

        }
    }

    setAlignTopGraph(preInfo, preTop, upBottomList) {
        let align2ThisBottomList = [];
        upBottomList.forEach(p => {
            let graph = p.graph;
            let verticalPostionCtrlFlag = graph.getVerticalPostionCtrlFlag();
            if (verticalPostionCtrlFlag == 0 || verticalPostionCtrlFlag == 2) {
                let detay = Math.abs(preInfo.max.y - p.min.y);
                if (detay < global.ZERO_4E) {
                    align2ThisBottomList.push(p);
                }
            }
        });

        align2ThisBottomList.forEach(p => {
            let graph = p.graph;
            let hornInfo = this.getGraphHornInfo(graph);
            let vertInfo = this.getGraphVertInfo(graph);
            let preHeight = Math.abs(vertInfo.max.y - vertInfo.min.y);

            vertInfo.min.y = preTop;
            if (graph.getVerticalSizeCtrlFlag() == 1) {
                vertInfo.max.y = vertInfo.min.y + preHeight;
            }

            this.restRectGraphPosition(graph, hornInfo, vertInfo);
            this.setAlignTopGraph(p, vertInfo.max.y, upBottomList);
        });
    }


    correctBottomGraphVeticalPosition(dragInfo, bottomGraph) {
        let basePt = dragInfo.basePt;
        let dragPt = dragInfo.dragPt;
        let preDragPt = dragInfo.preDragPt;

        let preDistance = Math.abs(preDragPt.y - basePt.y);
        let curentDistance = Math.abs(dragPt.y - basePt.y);
        let yscale = curentDistance / preDistance;

        let hornInfo = this.getGraphHornInfo(bottomGraph);
        let vertInfo = this.getGraphVertInfo(bottomGraph);
        let preHeight = Math.abs(vertInfo.max.y - vertInfo.min.y);

        let verticalPostionCtrlFlag = bottomGraph.getVerticalPostionCtrlFlag();
        if (verticalPostionCtrlFlag == 1 || verticalPostionCtrlFlag == 3) {
            let predis = vertInfo.min.y - basePt.y;
            let newy = basePt.y + predis * yscale;
            vertInfo.min.y = newy;
        }
        vertInfo.max.y = vertInfo.min.y + preHeight;
        this.restRectGraphPosition(bottomGraph, hornInfo, vertInfo);
        return vertInfo.max.y;
    }

    sortGraph(fixedWidList, kind, descendFlag) {
        let unsortedList = [];
        let sortedHornList = null;
        if (kind == 0) {
            fixedWidList.forEach(ele => {
                let t = this.getGraphHornInfo(ele);
                if (t) {
                    unsortedList.push(t);
                }
            });

            if (!descendFlag) {
                //升序
                sortedHornList = unsortedList.sort(function (p1, p2) {
                    if ((p1.min.x - p2.min.x) > 0) {
                        return 1;
                    }
                    else {
                        return -1;
                    }
                }
                );
            }
            else {
                //降序
                sortedHornList = unsortedList.sort(function (p1, p2) {
                    if ((p1.min.x - p2.min.x) < 0) {
                        return 1;
                    }
                    else {
                        return -1;
                    }
                }
                );
            }
        }
        else {
            fixedWidList.forEach(ele => {
                let t = this.getGraphVertInfo(ele);
                if (t) {
                    unsortedList.push(t);
                }
            });

            if (!descendFlag) {
                //升序
                sortedHornList = unsortedList.sort(function (p1, p2) {
                    if ((p1.min.y - p2.min.y) > 0) {
                        return 1;
                    }
                    else {
                        return -1;
                    }
                }
                );
            }
            else {
                //降序
                sortedHornList = unsortedList.sort(function (p1, p2) {
                    if ((p1.min.y - p2.min.y) < 0) {
                        return 1;
                    }
                    else {
                        return -1;
                    }
                }
                );
            }
        }
        console.log(sortedHornList);
        return sortedHornList;
    }

    getGraphHornInfo(graph) {
        let position = graph.getPosCopy();
        let minxPoint = null;
        let maxxPoint = null;
        if (position.posType == LGXEnums.LGXPosMode.multiPt_Mode) {
            let posList = position.posList;
            if (posList.length < 2) {
                return null;
            }
            minxPoint = posList[0];
            maxxPoint = posList[0];
            for (let i = 1; i < posList.length; i++) {
                let pt = posList[i];
                if (minxPoint.x > pt.x) {
                    minxPoint = pt;
                }

                if (maxxPoint.x < pt.x) {
                    maxxPoint = pt;
                }
            }
        }
        else {
            let rect = graph.getZone();
            minxPoint = { x: rect.left, y: rect.top };
            maxxPoint = { x: rect.right, y: rect.top };
        }
        let t = { graph, min: minxPoint, max: maxxPoint };
        return t;
    }

    getGraphVertInfo(graph) {
        let minyPoint = null;
        let maxyPoint = null;
        let position = graph.getPosCopy();
        if (position.posType == LGXEnums.LGXPosMode.multiPt_Mode) {
            let posList = position.posList;
            if (posList.length < 2) {
                return null;
            }
            minyPoint = posList[0];
            maxyPoint = posList[0];
            for (let i = 1; i < posList.length; i++) {
                let pt = posList[i];
                if (minyPoint.y > pt.y) {
                    minyPoint = pt;
                }

                if (maxyPoint.y < pt.y) {
                    maxyPoint = pt;
                }
            }
        }
        else {
            let rect = graph.getZone();
            minyPoint = { x: rect.left, y: rect.bottom };
            maxyPoint = { x: rect.left, y: rect.top };
        }
        let t = { graph, min: minyPoint, max: maxyPoint };
        return t;
    }



    //下面参数为作为模具图元时的控制参数
    //this.hornSizeCtrlFlag = 0;//1-尺寸不变 0-随模具主容器矩形 2-自由调整
    //this.hornPostionCtrlFlag = 0;//尺寸不变时横向位置控制参数，0-左侧跟随容器 1--右侧跟随容器

    //this.verticalSizeCtrlFlag = 0;//1-尺寸不变 0-随模具主容器矩形 2-自由调整
    //this.verticalPostionCtrlFlag = 0;//尺寸不变时横向位置控制参数，0-顶部跟随容器 1--底部跟随容器
    coorectAliginContainerChildPos(child, dragInfo) {
        if (!funcs.isBaseUnit(child.getType())) {
            return;
        }
        let hornSizeCtrlFlag = child.getHornSizeCtrlFlag();
        switch (hornSizeCtrlFlag) {
            case 0:
                {
                    if (dragInfo.extendKind == 0 || dragInfo.extendKind == 1) {
                        this.scaleGrapHorn(child, dragInfo);
                    }
                }
                break;
        }

        let verticalSizeCtrlFlag = child.getVerticalSizeCtrlFlag();
        switch (verticalSizeCtrlFlag) {
            case 0:
                {
                    if (dragInfo.extendKind == 0 || dragInfo.extendKind == 2) {
                        this.scaleGrapVertical(child, dragInfo);
                    }
                }
                break;
        }
    }

    scaleGrapHorn(graph, dragInfo) {
        let basePt = dragInfo.basePt;
        let dragPt = dragInfo.dragPt;
        let preDragPt = dragInfo.preDragPt;
        let preDistance = Math.abs(preDragPt.x - basePt.x);
        let curentDistance = Math.abs(dragPt.x - basePt.x);
        let scale = curentDistance / preDistance;

        let position = graph.getPosCopy();
        this.scaleGraphHornByPosMode(position, scale, basePt);
        graph.setPos(position);
    }

    scaleGrapVertical(graph, dragInfo) {
        let basePt = dragInfo.basePt;
        let dragPt = dragInfo.dragPt;
        let preDragPt = dragInfo.preDragPt;
        let preDistance = Math.abs(preDragPt.y - basePt.y);
        let curentDistance = Math.abs(dragPt.y - basePt.y);
        let scale = curentDistance / preDistance;

        let position = graph.getPosCopy();
        this.scaleGraphVerticalByPosMode(position, scale, basePt);
        graph.setPos(position);
    }

    scaleGraphHornByPosMode(position, scale, basePt) {
        switch (position.posType) {
            case LGXEnums.LGXPosMode.multiPt_Mode:
                this.scaleGraphHornMultiPosMode(position, scale, basePt);
                break;
        }
    }

    scaleGraphVerticalByPosMode(position, scale, basePt) {
        switch (position.posType) {
            case LGXEnums.LGXPosMode.multiPt_Mode:
                this.scaleGraphVerticalMultiPosMode(position, scale, basePt);
                break;
        }
    }

    scaleGraphHornMultiPosMode(position, scale, basePt) {
        let list = position.posList;
        for (let i = 0; i < list.length; i++) {
            let pt = list[i];
            let predis = pt.x - basePt.x;
            pt.x = basePt.x + predis * scale;
        }
    }

    scaleGraphVerticalMultiPosMode(position, scale, basePt) {
        let list = position.posList;
        for (let i = 0; i < list.length; i++) {
            let pt = list[i];
            let predis = pt.y - basePt.y;
            pt.y = basePt.y + predis * scale;
        }
    }

    //this.hornPostionCtrlFlag = 0;//尺寸不变时横向位置控制参数，0-左侧跟随容器 1--右侧跟随容器


    getGraphDragInfo(preRect, curentRect, selKind) {
        let basePt = null;
        let dragPt = null;
        let preDragPt = null;
        let extendKind = 0;
        switch (selKind) {
            case 1:
                basePt = preRect.rbPt();
                dragPt = curentRect.ltPt();
                preDragPt = preRect.ltPt();
                extendKind = 0;
                break;

            case 2:
                basePt = preRect.lbPt();
                dragPt = curentRect.rtPt();
                preDragPt = preRect.rtPt();
                extendKind = 0;
                break;

            case 3:
                basePt = preRect.ltPt();
                dragPt = curentRect.rbPt();
                preDragPt = preRect.rbPt();
                extendKind = 0;
                break;

            case 4:
                basePt = preRect.rtPt();
                dragPt = curentRect.lbPt();
                preDragPt = preRect.lbPt();
                extendKind = 0;
                break;

            case 5:
                basePt = preRect.rbPt();
                dragPt = curentRect.ltPt();
                preDragPt = preRect.ltPt();
                extendKind = 2;
                break;

            case 6:
                basePt = preRect.lbPt();
                dragPt = curentRect.rtPt();
                preDragPt = preRect.rtPt();
                extendKind = 1;
                break;

            case 7:
                basePt = preRect.rtPt();
                dragPt = curentRect.lbPt();
                preDragPt = preRect.lbPt();
                extendKind = 2;
                break;

            case 8:
                basePt = preRect.rbPt();
                dragPt = curentRect.ltPt();
                preDragPt = preRect.ltPt();
                extendKind = 1;
                break;
        }
        let dragInfo = null;
        if (basePt && dragPt && preDragPt) {
            dragInfo = { basePt, dragPt, extendKind, preDragPt };
        }
        return dragInfo;
    }



    getNoSpecialChild() {
        let list = [];
        let childList = this.getChildList();
        for (let i = 0; i < childList.length; i++) {
            let child = childList[i];
            let special = false;
            if (child.m_graphType == LGXEnums.LGXGraphType.RectGraph_type) {
                if (child.getSymbolEdgeFlag()) {
                    special = true
                }
            }
            else if (child.m_graphType == LGXEnums.LGXGraphType.TextGraph_type) {
                if (child.getParentGraph()) 
                {
                    special = true
                }
            }
            if (!special) {
                list.push(child);
            }
        }
        return list;
    }


    // <el-radio label=0 size="large">左侧随容器</el-radio>
    //     <el-radio label=1 size="large">右侧随容器</el-radio>
    //     <el-radio label=2 size="large">左侧保持横向布局对齐</el-radio>
    //     <el-radio label=3 size="large">右侧保持横向布局对齐</el-radio>
    correctHornPosition_alignLeft(dragInfo, otherChildList) {
        let unSortedAlignLeftGraphList = [];
        otherChildList.forEach(graph => {
            let hornPostionCtrlFlag = graph.getHornPostionCtrlFlag();
            if (hornPostionCtrlFlag == 0 || hornPostionCtrlFlag == 2) {
                unSortedAlignLeftGraphList.push(graph);
            }
        });

        let sortedAlignLeftGraphList = this.sortGraph(unSortedAlignLeftGraphList, 0, false);
        let length = sortedAlignLeftGraphList.length;
        if (length > 0) {
            let leftGroup = [];
            let leftInfo = sortedAlignLeftGraphList[0];
            leftGroup.push(leftInfo);

            let onRightList = [];
            sortedAlignLeftGraphList.forEach(p => {
                if (p != leftInfo) {
                    let deta_minx = Math.abs(leftInfo.min.x - p.min.x);
                    if (deta_minx < global.ZERO_4E) {
                        leftGroup.push(p);
                    }
                    else {
                        onRightList.push(p);
                    }
                }
            });
            leftGroup.forEach(p => {
                let leftGraph = p.graph;
                let preRight = this.correctLeftGraphHornPosition(dragInfo, leftGraph);
                this.setAlignRightGraph(p, preRight, onRightList);
            });
        }

    }

    correctAutoAlignWidthGraphRight(unSortedAlignLeftGraphInfoList) {
        let autoAlignWidthGraphList = [];
        unSortedAlignLeftGraphInfoList.forEach(graphInfo => {
            let hornSizeCtrlFlag = graphInfo.graph.getHornSizeCtrlFlag();
            if (hornSizeCtrlFlag == 2) {
                autoAlignWidthGraphList.push(graphInfo);
            }
        });

        autoAlignWidthGraphList.forEach(p => {
            this.correctAutoAlignWidthGraphRightRefOtherGraph(p, unSortedAlignLeftGraphInfoList);
        });
    }

    correctAutoAlignWidthGraphRightRefOtherGraph(curentGraphInfo, unSortedAlignLeftGraphInfoList) {
        let destAlignLeftGraph = null;//当前待调整为图形右侧坐标,找其右侧在未调整前对齐其他图形的左侧为目标
        unSortedAlignLeftGraphInfoList.forEach(p => {
            let deta = Math.abs(p.min.x - curentGraphInfo.max.x);
            if (deta < global.ZERO_4E) {
                destAlignLeftGraph = p.graph;
                return;
            }
        });
        if (destAlignLeftGraph) {
            let destHornInfo = this.getGraphHornInfo(destAlignLeftGraph);

            let curentGraph = curentGraphInfo.graph;
            let hornInfo = this.getGraphHornInfo(curentGraph);
            let vertInfo = this.getGraphVertInfo(curentGraph);

            hornInfo.max.x = destHornInfo.min.x;
            this.restRectGraphPosition(curentGraph, hornInfo, vertInfo);
        }
        else {
            //如果没有左侧和当前图形对齐的，则看看是不是跟外框右侧对齐了
            let info = this.m_preCurentModifingFrameHornInfo;
            let deta = Math.abs(info.max.x - curentGraphInfo.max.x);
            if (deta < global.ZERO_4E) {
                let destHornInfo = this.getGraphHornInfo(this.m_preCurentModifingFrameHornInfo.graph);

                let curentGraph = curentGraphInfo.graph;
                let hornInfo = this.getGraphHornInfo(curentGraph);
                let vertInfo = this.getGraphVertInfo(curentGraph);

                hornInfo.max.x = destHornInfo.max.x;
                this.restRectGraphPosition(curentGraph, hornInfo, vertInfo);
            }
        }
    }

    setAlignRightGraph(preInfo, preRight, onRightList) {
        let align2ThisRightList = [];
        onRightList.forEach(p => {
            let graph = p.graph;
            let hornPostionCtrlFlag = graph.getHornPostionCtrlFlag();
            if (hornPostionCtrlFlag == 0 || hornPostionCtrlFlag == 2) {
                let detax = Math.abs(preInfo.max.x - p.min.x);
                if (detax < global.ZERO_4E) {
                    align2ThisRightList.push(p);
                }
            }
        });

        align2ThisRightList.forEach(p => {
            let graph = p.graph;
            let hornInfo = this.getGraphHornInfo(graph);
            let vertInfo = this.getGraphVertInfo(graph);
            let preWidth = Math.abs(hornInfo.max.x - hornInfo.min.x);

            hornInfo.min.x = preRight;
            if (graph.getHornSizeCtrlFlag() == 1) {
                hornInfo.max.x = hornInfo.min.x + preWidth;
            }

            this.restRectGraphPosition(graph, hornInfo, vertInfo);
            this.setAlignRightGraph(p, hornInfo.max.x, onRightList);
        });
    }

    correctLeftGraphHornPosition(dragInfo, leftGraph) {
        let basePt = dragInfo.basePt;
        let dragPt = dragInfo.dragPt;
        let preDragPt = dragInfo.preDragPt;

        let preDistance = Math.abs(preDragPt.x - basePt.x);
        let curentDistance = Math.abs(dragPt.x - basePt.x);
        let xscale = curentDistance / preDistance;

        let hornInfo = this.getGraphHornInfo(leftGraph);
        let vertInfo = this.getGraphVertInfo(leftGraph);
        let preWidth = Math.abs(hornInfo.max.x - hornInfo.min.x);

        let hornPostionCtrlFlag = leftGraph.getHornPostionCtrlFlag();
        if (hornPostionCtrlFlag == 0 || hornPostionCtrlFlag == 2) {
            let predis = hornInfo.min.x - basePt.x;
            let newx = basePt.x + predis * xscale;
            hornInfo.min.x = newx;
        }
        if (leftGraph.getHornSizeCtrlFlag() == 1) {
            hornInfo.max.x = hornInfo.min.x + preWidth;
        }

        this.restRectGraphPosition(leftGraph, hornInfo, vertInfo);
        return hornInfo.max.x;
    }


    correctHornPosition_alignRight(dragInfo, otherChildList) {
        let unSortedAlignRightGraphList = [];
        otherChildList.forEach(graph => {
            let hornPostionCtrlFlag = graph.getHornPostionCtrlFlag();
            if (hornPostionCtrlFlag == 1 || hornPostionCtrlFlag == 3) {
                unSortedAlignRightGraphList.push(graph);
            }
        });
        let sortedAlignRightGraphList = this.sortGraph(unSortedAlignRightGraphList, 0, true);
        let length = sortedAlignRightGraphList.length;
        if (length > 0) {
            let rightGroup = [];
            let rightInfo = sortedAlignRightGraphList[0];
            rightGroup.push(rightInfo);

            let onLeftList = [];
            sortedAlignRightGraphList.forEach(p => {
                if (p != rightInfo) {
                    let deta_maxx = Math.abs(bottomInfo.max.x - p.max.x);
                    if (deta_maxx < global.ZERO_4E) {
                        rightGroup.push(p);
                    }
                    else {
                        onLeftList.push(p);
                    }
                }
            });
            rightGroup.forEach(p => {
                let rightGraph = p.graph;
                let preLeft = this.correctRightGraphHornPosition(dragInfo, rightGraph);
                this.setAlignLeftGraph(p, preLeft, onLeftList);
            });

        }
    }

    correctRightGraphHornPosition(dragInfo, rightGraph) {
        let basePt = dragInfo.basePt;
        let dragPt = dragInfo.dragPt;
        let preDragPt = dragInfo.preDragPt;

        let preDistance = Math.abs(preDragPt.x - basePt.x);
        let curentDistance = Math.abs(dragPt.x - basePt.x);
        let xscale = curentDistance / preDistance;

        let hornInfo = this.getGraphHornInfo(rightGraph);
        let vertInfo = this.getGraphVertInfo(rightGraph);
        let preWidth = Math.abs(hornInfo.max.x - hornInfo.min.x);

        let hornPostionCtrlFlag = rightGraph.getHornPostionCtrlFlag();
        if (hornPostionCtrlFlag == 1 || hornPostionCtrlFlag == 3) {
            let predis = hornInfo.max.x - basePt.x;
            let newx = basePt.x + predis * xscale;
            hornInfo.max.x = newx;
        }
        if (rightGraph.getHornSizeCtrlFlag() == 1) {
            hornInfo.min.x = hornInfo.max.x - preWidth;
        }
        this.restRectGraphPosition(rightGraph, hornInfo, vertInfo);
        return hornInfo.min.x;
    }

    setAlignLeftGraph(preInfo, preLeft, onLeftList) {
        let align2ThisLeftList = [];//未调整前对齐当前图形左侧的其他图形队列
        onLeftList.forEach(p => {
            let graph = p.graph;
            let hornPostionCtrlFlag = graph.getHornPostionCtrlFlag();
            if (hornPostionCtrlFlag == 1 || hornPostionCtrlFlag == 3) {
                let detax = Math.abs(preInfo.min.x - p.max.x);
                if (detax < global.ZERO_4E) {
                    align2ThisLeftList.push(p);
                }
            }
        });

        align2ThisLeftList.forEach(p => {
            let graph = p.graph;
            let hornInfo = this.getGraphHornInfo(graph);
            let vertInfo = this.getGraphVertInfo(graph);
            let preWidth = Math.abs(hornInfo.max.x - hornInfo.min.x);

            hornInfo.max.x = preLeft;
            if (graph.getHornSizeCtrlFlag() == 1) {
                hornInfo.min.x = hornInfo.max.x - preWidth;
            }
            this.restRectGraphPosition(graph, hornInfo, vertInfo);
            this.setAlignLeftGraph(p, hornInfo.min.x, onLeftList);
        });
    }

    restRectGraphPosition(graph, hornInfo, vertInfo) {
        let position = graph.getPosCopy();
        if (graph.m_graphType == LGXEnums.LGXGraphType.RectGraph_type) {
            position.clear();
            let p1 = { x: hornInfo.min.x, y: vertInfo.max.y };
            position.addPoint(p1);

            let p2 = { x: hornInfo.max.x, y: vertInfo.max.y };
            position.addPoint(p2);

            let p3 = { x: hornInfo.max.x, y: vertInfo.min.y };
            position.addPoint(p3);

            let p4 = { x: hornInfo.min.x, y: vertInfo.min.y };
            position.addPoint(p4);

            graph.setPos(position);
        }
    }
}

export default { ContainerMoldGraph }
